<?php	
require_once("config_inc.php");

require_once("core/user_api.php");
require_once("core/gui_api.php");
require_once("core/email_api.php");

session_start();

if(isset($_POST["sign_up"]))
{	
	$db = new database();
	$db->connect();

	$user_api = new user_api($db);
	
	if($user_api->validate_new_user(trim($_POST["user_email"])))	
	{
		$user_api->create_user( $_POST["user_name"], $_POST["user_password"], $_POST["user_email"] );		
		
		$email_api = new email_api();				
		$email_api->send_validation_email( $_POST["user_email"], $user_api->generate_hash_code($_POST["user_email"]), $_POST["user_name"] );						
	}
	
	$db->close();
}

if(isset($_POST["sign_in"]))
{	
	$db = new database();
	$db->connect();

	$user_api = new user_api($db);
	$user_data = $user_api->login_user( $_POST["user_email"], $_POST["user_password"] );
			
	if($user_data == 0)
	{
		$login_status_error = -1;
	}
	else if($user_data[0] > 0 && $user_data[1] == USER_STATUS_ACTIVE)
	{
		$db->close();	
	
		$_SESSION["user_id"] = $user_data[0];
		$_SESSION["app"] = "share";
		
		header("Location: " . SERVER_URL);
		exit();
	}
}

if(isset($_POST["recover_password"]))
{	
	$email_api = new email_api();	
	$email_api->send_recorver_password($_POST["user_email"]);
}

if(isset($_POST["reset_password"]))
{	
	$db = new database();
	$db->connect();

	$user_api = new user_api($db);
	$user_id = $user_api->get_user_id($_POST["email"]);
	
	$user_api->set_user_password($user_id, $_POST["user_password"]);
		
	$db->close();
}

if(isset($_GET["control"]) && $_GET["control"] == "logout")
{		
	$db = new database();
	$db->connect();

	$user_api = new user_api(NULL);
	$user_api->logout_user();
	
	$db->close();
	
	header("Location: " . SERVER_URL);
	exit();
}

$db = new database();
$db->connect();

$gui_api = new gui_api($db);

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<?php require_once("meta_inc.php"); ?>
<script type="text/javascript" src="<?=SERVER_URL?>js/jquery.validate.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
	$("#signup_form").validate({
		errorPlacement: function(error, element) {
			error.appendTo( element.parent("td").next("td") );
		},
		rules: {
			user_email: {required:true, email: true},
			user_password: {required:true, minlength:5}
		}
	});
	
	$("#signin_form").validate({
		errorPlacement: function(error, element) {
			error.appendTo( element.parent("td").next("td") );
		},
		rules: {
			user_email: {required:true, email: true},
			user_password: {required:true, minlength:5},
			user_name: {required:true, minlength:3}
		}
	});		
	
	$("#recover_form").validate({
		errorPlacement: function(error, element) {
			error.appendTo( element.parent("td").next("td") );
		},
		rules: {
			user_email: {required:true, email: true},
		}
	});	
	
	$("#reset_form").validate({
		errorPlacement: function(error, element) {
			error.appendTo( element.parent("td").next("td") );
		},
		rules: {
			user_password: {required:true},
			user_reply_password: {required:true, equalTo: "#user_password"}			
		}
	});
	
});
</script>

</head>
<body>

<?php require_once("topbar_inc.php"); ?> 

<div class="wrapper" style="margin-top:80px">
	    
    <div class="left-column-right">
    	<div class="small-white-page input-form">
			<div class="sub-gray-title">recommended</div>
			<?=$gui_api->insert_feed_list(FEED_TYPE_RECOMENDED, 6);?>
		</div>
    </div>
    
    <div class="right-column-right">    	
		<div class="big-white-page">
		<?php
			if(isset($_POST["sign_in"]))
			{
				switch($login_status_error)
				{
				case -1:
					?><p class="message-box message-alert">invalid e-mail or password.</p><?php
					break;
				}
			}		
		?>
		
		
		<?php if(isset($_GET["control"]) && $_GET["control"] == "recover_password") { ?>
        <form action="<?=SERVER_URL?>recover_password" method="post" id="recover_form">
        <table cellpadding="5" cellspacing="5" class="input-form">
            <tr>
                <td><h1>recover password</h1></td>
            </tr>
            <tr>                    
                <td><input type="email" name="user_email" placeholder="your e-mail" /></td>                
				<td></td>
            </tr>
            <tr>                    
                <td><input type="submit" value="recover password" name="recover_password" /></td>
            </tr>
        </table>
        </form>
        <?php } ?>
		
		<?php if(isset($_GET["control"]) && $_GET["control"] == "reset_password") { ?>
        <form action="<?=SERVER_URL?>reset_password/<?=$_GET["email"]?>/<?=$_GET["hash"]?>" method="post" id="reset_form">
        <table cellpadding="5" cellspacing="5" class="input-form">
            <tr>
                <td><h1>reset password</h1></td>
            </tr>
            <tr>                    
                <td><input type="password" id="user_password" name="user_password" placeholder="your password" /></td>                
				<td></td>
            </tr>
			<tr>                    
                <td><input type="password" name="user_reply_password" placeholder="reply password" /></td>                
				<td></td>
            </tr>
            <tr>                    
                <td><input type="submit" value="set new password" name="reset_password" /></td>
            </tr>
        </table>
        </form>
        <?php } ?>

		<?php if(isset($_GET["control"]) && $_GET["control"] == "login") { ?>
        <form action="<?=SERVER_URL?>login" method="post" id="signup_form">
        <table cellpadding="5" cellspacing="5" class="input-form">
            <tr>
                <td colspan="2"><h1>sign in!</h1></td>
            </tr>
            <tr>                    
                <td colspan="2"><input type="email" name="user_email" placeholder="your e-mail" /></td>                
                <td></td>
            </tr>
            <tr>                    
                <td colspan="2"><input type="password" placeholder="your password" name="user_password" /></td>
                <td></td>
            </tr>
            <tr>                    
                <td><input type="submit" value="sign in" name="sign_in" /></td>
                <td valign="top" align="right"><a class="small-link" href="<?=SERVER_URL?>recover_password">forgot password?</a></td>
            </tr>
        </table>
        </form>
        <form action="<?=SERVER_URL?>login" method="post" id="signin_form">
        <table cellpadding="5" cellspacing="5" class="input-form">
            <tr>
                <td><h1>don't have an account?</h1></td>
            </tr>  
			<tr>                    
                <td><input type="text" placeholder="your name" name="user_name" /></td>
				<td></td>
            </tr>
			<tr>
                <td><input type="email" placeholder="your e-mail" name="user_email" /></td>
                <td></td>
            </tr>
			<tr>                    
                <td><input type="password" placeholder="your password" name="user_password" /></td>
				<td></td>
            </tr>                        			
			<tr>
                <td><input type="submit" value="register" name="sign_up" /></td>
            </tr>
        </table>
        </form>
        <?php } ?>
         
    	<?php 
			if(isset($_GET["control"]) && $_GET["control"] == "register") 
			{ 						
				$user_api = new user_api($db);		
				
				if($_GET["hash"] == $user_api->generate_hash_code($_GET["email"]))
				{
					$user_id = $user_api->get_user_id($_GET["email"]);
					$user_api->set_user_status($user_id, USER_STATUS_ACTIVE);					
				}
				else
				{
				}							
			} 		
		?>         
    	</div>
    </div>
    
    <div class="clear"></div>   
</div>   

<?php require_once("bottombar_inc.php"); ?>	

</body>
</html>
<?php $db->close(); ?>